<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>车间基础建模</el-breadcrumb-item>
      <el-breadcrumb-item>资源维护</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row :gutter="10">
        <el-col :span="7">
          <el-input placeholder="请输入资源编号检索" v-model="query">
            <template #append>
              <el-button icon="el-icon-search" @click="getList">检索</el-button>
            </template>
          </el-input>
        </el-col>
        <el-col span="4">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="addDialogVisible = true"
            >新增</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table
        stripe
        border
        ref="multipleTable"
        :data="operationResourceList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="id" label="id" width="160" align="center">
        </el-table-column>
        <el-table-column
          prop="operationResource_no"
          label="资源编号"
          width="240"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="operationResource_desc"
          label="资源描述"
          width="260"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="operation"
          label="默认操作"
          width="225"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="site"
          label="挂靠站点"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="byUser"
          label="创建人"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="260"
          align="center"
        >
          <template #default="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center" fixed="right">
          <template #default="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editDialog(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="del(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNow"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="rowCount"
      >
      </el-pagination>
    </el-card>
    <!-- 添加对话框 -->
    <el-dialog title="新增资源" v-model="addDialogVisible" width="40%">
      <!-- 新增默认操作二级对话框 -->
      <el-dialog
        title="操作列表"
        v-model="operationDV"
        width="40%"
        append-to-body
      >
        <!-- 对话框内容 -->
        <el-table
          stripe
          border
          ref="multipleTable"
          :data="operationList"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          size="small"
        >
          <el-table-column prop="id" label="id" width="50" align="center">
          </el-table-column>
          <el-table-column
            prop="operation_no"
            label="操作编号"
            width="80"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="operation_description"
            label="操作描述"
            width="80"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="operation_type"
            label="操作类型"
            width="80"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="resoucre_type"
            label="资源类型"
            width="80"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="default_resource"
            label="默认资源"
            width="80"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="site"
            label="挂靠站点"
            width="80"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="byUser"
            label="创建人"
            width="80"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="创建时间"
            width="80"
            align="center"
            show-overflow-tooltip
          >
          <template #default="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
          </el-table-column>
          <el-table-column label="操作" width="95" align="center">
            <template #default="scope">
              <el-button size="mini" @click="addOperation(scope.row)"
                >选择</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNow"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="rowCount"
        >
        </el-pagination>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="operationDV = false">取 消</el-button>
          </span>
        </template>
      </el-dialog>
      <!-- 二级对话框结束 -->
      <!-- 对话框内容 -->
      <el-form
        :model="addForm"
        status-icon
        :rules="addFormrules"
        ref="addFormRef"
        label-width="120px"
      >
        <el-form-item label="所属站点" prop="site">
          <el-input v-model="addForm.site" disabled placeholder="请选择所属站点"></el-input>
        </el-form-item>
        <el-form-item label="资源编号" prop="operationResource_no">
          <el-input v-model="addForm.operationResource_no" placeholder="请输入资源编号"></el-input>
        </el-form-item>
        <el-form-item label="资源描述" prop="operationResource_desc">
          <el-input v-model="addForm.operationResource_desc" placeholder="请输入资源描述"></el-input>
        </el-form-item>
        <el-form-item label="默认操作" prop="operation">
          <el-input v-model="addForm.operation" placeholder="请选择默认操作">
          <template #suffix>
          <i class="el-input__icon el-icon-search" @click="addoperation"></i>
          </template>
          </el-input>
        </el-form-item>
        <el-form-item label="是否可用" prop="state">
          <el-select
            v-model="addForm.state"
            placeholder="请选择是否可用"
            
          >
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="add">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 编辑对话框 -->
    <el-dialog title="编辑资源" v-model="editDialogVisible" width="40%">
      <!-- 编辑默认操作二级对话框 -->
      <el-dialog
        title="操作列表"
        v-model="operationDV"
        width="40%"
        append-to-body
      >
        <!-- 对话框内容 -->
        <el-table
          stripe
          border
          ref="multipleTable"
          :data="operationList"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          size="small"
        >
          <el-table-column prop="id" label="id" width="50" align="center">
          </el-table-column>
          <el-table-column
            prop="operation_no"
            label="操作编号"
            width="80"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="operation_description"
            label="操作描述"
            width="80"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="operation_type"
            label="操作类型"
            width="80"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="resoucre_type"
            label="资源类型"
            width="80"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="default_resource"
            label="默认资源"
            width="80"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="site"
            label="挂靠站点"
            width="80"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="byUser"
            label="创建人"
            width="80"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="创建时间"
            width="80"
            align="center"
            show-overflow-tooltip
          >
          <template #default="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
          </el-table-column>
          <el-table-column label="操作" width="95" align="center">
            <template #default="scope">
              <el-button size="mini" @click="editOperation(scope.row)"
                >选择</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNow"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="rowCount"
        >
        </el-pagination>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="operationDV = false">取 消</el-button>
          </span>
        </template>
      </el-dialog>
      <!-- 二级对话框结束 -->
      <!-- 对话框内容 -->
      <el-form
        :model="editForm"
        status-icon
        :rules="addFormrules"
        ref="editFormRef"
        label-width="120px"
      >
        <el-form-item label="所属站点" prop="site">
          <el-input v-model="editForm.site" disabled placeholder="请选择默认站点"></el-input>
        </el-form-item>
        <el-form-item label="资源编号" prop="operationResource_no">
          <el-input v-model="editForm.operationResource_no" placeholder="请输入资源编号"></el-input>
        </el-form-item>
        <el-form-item label="资源描述" prop="operationResource_desc">
          <el-input v-model="editForm.operationResource_desc" placeholder="请输入资源描述"></el-input>
        </el-form-item>
        <el-form-item label="默认操作" prop="operation">
          <el-input v-model="editForm.operation" placeholder="请选择默认操作">
          <template #suffix>
          <i class="el-input__icon el-icon-search" @click="editoperation"></i>
          </template>
          </el-input>
        </el-form-item>
        <el-form-item label="是否可用" prop="state">
          <el-select
            v-model="editForm.state"
            placeholder="请选择是否可用"
            
          >
            <el-option label="是" :value="1"></el-option>
            <el-option label="否" :value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="edit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      query: "",
      pageCount: 1,
      pageNow: 1,
      pageSize: 10,
      rowCount: 0,
      operationResourceList: [],
      // 显示添加对话框
      addDialogVisible: false,
      // 显示编辑对话框
      editDialogVisible: false,
      // 默认操作二级对话框
      operationDV:false,
      addForm: {
        site: "*",
        operationResource_no: "",
        operationResource_desc: "",
        operation: "",
        state: "",
      },
      editForm: {},
      addFormrules: {
        site: [],
        operationResource_no: [
          {
            required: true,
            message: "请输入资源编号",
            trigger: "blur",
          },
        ],
        operationResource_desc: [
          {
            required: true,
            message: "请输入资源描述",
            trigger: "blur",
          },
        ],
        operation: [
          {
            required: true,
            message: "请输入操作",
            trigger: "blur",
          },
        ],
        state: [
          {
            required: true,
            message: "请选择是否可用",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      this.$axios({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        method: "post",
        url: "/IE_MES/workshop/operation_resource_findByPage.shtml",
        data: this.$qs.stringify({
          pageSize: this.pageSize,
          pageNow: this.pageNow,
          "operationResourceFormMap.operationResource_no": this.query,
        }),
      }).then((res) => {
        console.log(res.data);
        this.operationResourceList = res.data.records;
        this.pageCount = res.data.pageCount;
        this.pageNow = res.data.pageNow;
        this.pageSize = res.data.pageSize;
        this.rowCount = res.data.rowCount;
      });
    },
    // 新增
    add() {
      this.$axios({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        method: "post",
        url: "/IE_MES/workshop/operation_resource_addEntity.shtml",
        data: this.$qs.stringify({
          "operationResourceFormMap.site": this.addForm.site,
          "operationResourceFormMap.operationResource_no":
            this.addForm.operationResource_no,
          "operationResourceFormMap.operationResource_desc":
            this.addForm.operationResource_desc,
          "operationResourceFormMap.operation": this.addForm.operation,
          "operationResourceFormMap.state": this.addForm.state,
        }),
      }).then((res) => {
        if (res.data == '{"result":"success","score":""}') {
          this.getList();
          this.$refs['addFormRef'].resetFields();
          this.addDialogVisible = false;
          this.$message({
            type: "success",
            message: "添加成功!",
          });
        }
      });
    },
    addoperation(){
      this.operationDV = true;
      this.$axios({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        method: "post",
        url: "/IE_MES/workshop/operation_findByPage.shtml",
        data: this.$qs.stringify({
          pageSize: this.pageSize,
          pageNow: this.pageNow,
        }),
      }).then((res) => {
        console.log(res.data);
        this.operationList = res.data.records;
        this.pageCount = res.data.pageCount;
        this.pageNow = res.data.pageNow;
        this.pageSize = res.data.pageSize;
        this.rowCount = res.data.rowCount;
      });
    },
    addOperation(row) {
      this.addForm.operation = row.operation_no;
      this.operationDV = false;
    },
    // 编辑
    editDialog(row) {
      this.editForm = JSON.parse(JSON.stringify(row));
      this.editDialogVisible = true;
    },
    edit() {
      this.$axios({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        method: "post",
        url: "/IE_MES/workshop/operation_resource_editEntity.shtml",
        data: this.$qs.stringify({
          "operationResourceFormMap.id": this.editForm.id,
          "operationResourceFormMap.site": this.editForm.site,
          "operationResourceFormMap.operationResource_no":
            this.editForm.operationResource_no,
          "operationResourceFormMap.operationResource_desc":
            this.editForm.operationResource_desc,
          "operationResourceFormMap.operation": this.editForm.operation,
          "operationResourceFormMap.state": this.editForm.state,
        }),
      }).then((res) => {
        if (res.data == "success") {
          this.getList();
          this.editDialogVisible = false;
          this.$message({
            type: "success",
            message: "编辑成功!",
          });
        }
      });
    },
    editoperation(){
      this.operationDV = true;
      this.$axios({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        method: "post",
        url: "/IE_MES/workshop/operation_findByPage.shtml",
        data: this.$qs.stringify({
          pageSize: this.pageSize,
          pageNow: this.pageNow,
        }),
      }).then((res) => {
        console.log(res.data);
        this.operationList = res.data.records;
        this.pageCount = res.data.pageCount;
        this.pageNow = res.data.pageNow;
        this.pageSize = res.data.pageSize;
        this.rowCount = res.data.rowCount;
      });
    },
    editOperation(row) {
      this.editForm.operation = row.operation_no;
      this.operationDV = false;
    },
    // 删除
    del(row) {
      this.$confirm("此操作将永久删除该资源, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios({
            headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
            method: "post",
            url: "/IE_MES/workshop/operation_resource_deleteEntity.shtml?_t="+new Date(),
            data: this.$qs.stringify({
              ids:row.id
            }),
          }).then((res) => {
            if (res.data == "success") {
              this.getList();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.getList();
    },
    handleCurrentChange(newPage) {
      this.pageNow = newPage;
      this.getList();
    },
    formatDate(data) {
      var date = new Date(data);
      var YY = date.getFullYear() + "-";
      var MM =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      var hh =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      var mm =
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        ":";
      var ss =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return YY + MM + DD + " " + hh + mm + ss;
    },
  },
};
</script>

<style lang="less" scoped>
.el-form-item /deep/ .el-input__inner {
  padding-right: 420px !important;
}
</style>
